<?php

namespace App\Models;

use App\Models\Base;
use Illuminate\Database\Eloquent\SoftDeletes;
use DB;

class Message extends Base
{
    protected $table = 'message';
    protected $hidden = ['deleted_at'];

    use SoftDeletes;

    public function add_data($data){
        //添加数据
        $result=$this
            ->create($data)
            ->id;
        return $result;
    }

    public function edit_data($id,$data){
        $result = $this->where('id',hashid_decode($id))->update($data);
        if (!$result) return false;
        return true;
    }

    public function getDataById($id){
        $result = $this
            ->where('id',hashid_decode($id))
            ->first();
        return $result;
    }

    public function getDataByMap($params){
        $type = array_key_exists('type',$params)?$params['type']:false;
        $result = $this
            ->orderBy('created_at','desc')
            ->paginate(20)
            ->toArray();
        $data = $result['data'];
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $data[$k]['id'] = hashid_encode($v['id']);
            }
            $result['data'] = $data;
        }
        return $result;
    }

    public function delData($id){
        $result = $this->where('id',hashid_decode($id))->delete();
        return $result;
    }

}
